<?php

declare(strict_types=1);
namespace app\core;


class File
{
    protected $_rootpath='';
    protected $_filepath='';
    protected $_basepath='';
    protected $fn='';
    protected $dfn='';
    public function __construct()
    {
        $this->_rootpath=app()->getRootPath();
        $this->_basepath=$this->_rootpath.'runtime/filecache';
        $this->_filepath=$this->_basepath;
        $this->setMkdir();
        $this->init();
    }

    // 初始化
    protected function init()
    {}

    public function setPath($dir){
        $this->_basepath=$dir;
        $this->_filepath=$this->_basepath;
        $this->setMkdir();
        
    }

    public function setDir($dir){
        $this->_basepath=$this->_basepath.'/'.$dir;
        $this->_filepath=$this->_basepath;
        $this->setMkdir();
        
    }

    //创建目录
    protected function setMkdir(){
        if(!is_dir($this->_filepath)){
            $dir = explode($this->_rootpath,$this->_filepath);
            if($dir[1]){
                $dlist=explode('/',$dir[1]);
                $n=count($dlist);
                $mydir=$this->_rootpath;
                for($i=0;$i<$n;$i++){
                    if($i==0){
                        $mydir .=$dlist[$i];
                    }else{
                        $mydir .='/'.$dlist[$i];
                    }
                    $this->saveDir($mydir);
                }
            }
        }
    }

    //创建可读可写目录
    private function saveDir($dir){
        if(!is_dir($dir)){
            mkdir($dir);
            chmod($dir,0777);
        }
    }

    //文件保存
    public function filesave($filename,$wstr,$type='w'){
        $filename=$this->_filepath.'/'.$filename;
        $fp = fopen($filename,$type);
        $fw = fwrite($fp,$wstr);
        $fc = fclose($fp);
    }

    public function Log($str,$fn=''){
        $this->_filepath=$this->_basepath.'/log'.'/'.date("Y-m-d");
        $this->setMkdir();
        $filename=date("Y-m-d").'.txt';
        if($fn!=''){
            $filename=$fn;
        }
        $str=PHP_EOL.date("Y-m-d H:i:s",time()).PHP_EOL.$str.PHP_EOL.'***************************';
        $this->filesave($filename,$str,'a');
    }

    public function getArrayData($name,$outtime=0){
        $this->fn=md5($name).'.json';
        $this->dfn=$this->_filepath.'/'.$this->fn;
        $list=[];
        if($outtime>0){
            if(file_exists($this->dfn)){
                $ftime=filemtime($this->dfn);
                if((time()-$ftime)>$outtime){
                    unlink($this->dfn);
                }else{
                    $json=$this->getFileContents($this->dfn);
                    $list=json_decode($json,true);
                }
            }
        }
        return $list;
    }

    //获取文件内容
    public function getFileContents($filename){
        $handle = fopen($filename, "r");
        $contents = fread($handle, filesize ($filename));
        fclose($handle);
        return $contents;
    }

    public function setArrayData($arr){
        $jsonencode = json_encode($arr,JSON_UNESCAPED_UNICODE);
        $this->saveFile($jsonencode);
    }

    public function saveFile($str){
        $this->filesave($this->fn,$str);
        
    }

    public function getCache($name,$outtime=0){
        $this->_filepath=$this->_basepath.'/cache';
        $this->setMkdir();
        return $this->getArrayData($name,$outtime);
    }

    public function setCache($name,$arr){
        $this->_filepath=$this->_basepath.'/cache';
        $this->setMkdir();
        $this->fn=md5($name).'.json';
        $this->setArrayData($arr);
    }


}